<?php
@set_time_limit(0);
@session_start();
error_reporting (E_ALL ^ E_NOTICE);

require("../../includes/config.inc.php"); 
require("../../includes/Database.class.php"); 


require_once("../../includes/GPSFunction.php");

$db = new Database(DB_SERVER, DB_USER, DB_PASS, DB_DATABASE); 
$db->connect();

$temp="0";
//print_r($_GET);
function chk_folder($filename)
{
	$fp_load = @fopen("$filename", "rb");
	if ( $fp_load )
	{
		return true;
	}
	else
	{
		return false;
	}
}
function liveKmsAllPerDay($lData)
{ 
	$timeArr = array();
	$ptArr = array();
	$tmpDist = -1;
	//$ptArr = array();
	$cnt = 1;
	$totalDistance = 0;
	$data1=explode("#",$lData);
	for($j1=0;$j1<count($data1);$j1++)
	{
		$data2=explode("@",$data1[$j1]);

		if(count($data2)>1)
		{
			$data3=explode(",",$data2[1]);
			$geodate = $data3[8];
			$geoTime = $data3[9];
			$curTime = explode(":",$data3[9]);
			$curTime = (($curTime[0] * 60) + $curTime[1]);
			//echo "<br>";

			$pos1=calLat($data3[2]);
			$pos2=calLong($data3[1]);
			
			if(($pos1>0 && $pos2>0))
			{
				
				if(!in_array($geoTime,$timeArr))
				{
					$ptArr[]= $pos1.",".$pos2;
					if($j1==0)
					{
						$pits1 =  $pos1;
						$pits2 =  $pos2;
					}
					else
					{
						$pits3 =  $pos1;
						$pits4 =  $pos2;
						
						$dist = getDistance($pits1, $pits2, $pits3, $pits4);
						$totalDistance += $dist;						
						$pits1 =  $pits3;
						$pits2 =  $pits4;
					}
				}
				
			}
		}
		array_push($timeArr,$geoTime);
		//exit;
	}
	//echo $j1;
	//echo round($totalDistance,2); 
	$ptArr = implode("@",$ptArr); 
	//echo $ptArr;
	//exit;
	$finalData = round($totalDistance)."#".$ptArr;
	return $finalData;
}
function liveKmsPerDay($lData,$sTime1,$eTime1)
{ 
	$timeArr = array();
	$ptArr = array();
	//$ptArr = array();
	$cnt = 0;
	$totalDistance = 0;
	$data1=explode("#",$lData);
	for($j1=0;$j1<count($data1);$j1++)
	{
		$data2=explode("@",$data1[$j1]);

		if(count($data2)>1)
		{
			$data3=explode(",",$data2[1]);
			$geodate = $data3[8];
			$geoTime = $data3[9];
			$curTime = explode(":",$data3[9]);
			$curTime = (($curTime[0] * 60) + $curTime[1]);
			//echo "<br>";
			//echo $sTime1.">=".$data3[9]." ".$curTime."<=".$eTime1."<br>";
			if($curTime >= $sTime1 && $curTime<=$eTime1)
			{
			//echo $geoTime." ".$geodate." ".$sTime1.">=".$curTime."<=".$eTime1."<br>";
			$pos1=calLat($data3[2]);
			$pos2=calLong($data3[1]);
			
			if($pos1>0 && $pos2>0)
			{
				
				if(!in_array($geoTime,$timeArr))
				{
					$tmpLatLng= $pos1.",".$pos2;
					if(!in_array($tmpLatLng,$ptArr))
					{	
						$ptArr[] = $tmpLatLng;
					}
					//echo $cnt;
					if($cnt==0)
					{
						$pits1 =  $pos1;
						$pits2 =  $pos2;
					}
					else
					{
						$pits3 =  $pos1;
						$pits4 =  $pos2;
						
						$dist = getDistance($pits1, $pits2, $pits3, $pits4);
						
						$totalDistance += $dist;
						$pits1 =  $pits3;
						$pits2 =  $pits4;
					}
				}
			}
				$cnt++;
			}
		}
		array_push($timeArr,$geoTime);
	}
	//echo $j1;
	//echo count($ptArr);
	$ptArr = implode("@",$ptArr);
	$finalData = round($totalDistance)."#".$ptArr;
	return $finalData;
}

function gpspathFun($clientId,$date_offline,$deviceIMEI,$sTime,$eTime)
{
	//$date_offline = $_GET[date_offline];
	
	$path1=DATAPATH.date('d-m-Y',strtotime($date_offline))."/".$deviceIMEI.".txt"; ;
	
	if(chk_folder($path1))
	{
		$file1 = @fopen($path1, "r");
		if($file1)
		{
			$i=0;
			while(!feof($file1))
			{
			  $data1= fgets($file1);				 
			}
			$data = getSortedData($data1);
			return $data;
			fclose($file1);
		}
		else
		{
			$data=0;
			//storeData($data);
		}
	}
		
} //$path1=$dataPath."client_".$_SESSION[clientID]."/".date('d-m-Y',strtotime($_GET["date_offline"]))."/".$_GET["deviceIMEI"].".txt";
if(isset($_GET[sDate]) && isset($_GET[sTime]) && isset($_GET[eDate]) && isset($_GET[eTime]) && isset($_GET[deviceIMEI]))
{

	$devData = explode("^",getDeviceName($_GET["deviceIMEI"]));  
	$devName = $devData[0];
	$devModel = $devData[1];
	$devIcon = $devData[5];
	$inPort = $devData[2];
	$outPort = $devData[3];
	$analogPort = $devData[4];

	if($_GET[sDate] == $_GET[eDate])
	{
		$data = gpspathFun($_SESSION[clientID],$_GET[sDate],$_GET["deviceIMEI"],$_GET[sTime],$_GET[eTime]);	
		
		$data1=explode("#",$data);

		$xml = '<gps>';
		$timeArr= array();
		$ptArr = array();
		$ptNewArr = array();
		$totalDistance=0;
		
		$ct = 0;
		$sstime = "";
		$j1 = 0;
		$tmpDist = -1;
		
		while($j1<count($data1))
		{
			$data2=explode("@",$data1[$j1]);
			if(count($data2)>1)
			{
				$data3=explode(",",$data2[1]);
				$geoTime = date("h:i:s A",strtotime($data3[9]));
				$geodate = $data3[8]." ".$geoTime;
				$curTime = explode(":",$data3[9]);
				$curTime = (($curTime[0] * 60) + $curTime[1]);
				$pos1=calLat($data3[2]);
				$pos2=calLong($data3[1]);
			
				if($curTime >= $_GET[sTime] && $curTime<=$_GET[eTime])
				{
					if($pos1>0 && $pos2>0)
					{						
						if(!in_array($geoTime,$timeArr))
						{
							if($ct==0)
							{									
								$pits1 =  $pos1;
								$pits2 =  $pos2;
							}
							else
							{
								$pits3 =  $pos1;
								$pits4 =  $pos2;
								$dist = getDistance($pits1, $pits2, $pits3, $pits4);
								$totalDistance += $dist;
								$pits1 =  $pits3;
								$pits2 =  $pits4;
							}
							$mph = round($data3[3]);
							$direction = $data3[4];
							$altitute = $data3[5];
							$deviceIMEI = $data3[0];
							$sessionID = $_GET["sessionID"] ;
							$accuracy = $data3[6];
							$extraInfo = $data3[6];
							$travlDist = round($totalDistance,1);
							
							//echo $inPort.','.$outPort.','.$analogPort;		//
							/*if($inPort!='')
							{
								$inPort1 = explode("#",$inPort);
								for($k=0;$k<count($inPort1)-1;$k++)
								{						
									$inp = explode("_",$inPort1[$k]);
									$digInputLabel .= $inp[0]."#";
									$digInputValue .= splitParameter($inp[1],$data3[7])."#";	
								}
							}
							
							if($outPort!='')
							{
								$outPort1 = explode("#",$outPort);
								for($k=0;$k<count($outPort1)-1;$k++)
								{						
									$out = explode("_",$outPort1[$k]);
									$digOutputLabel .= $out[0]."#";
									$digOutputValue .= splitParameter($out[1],$data3[7])."#";	
								}
							}
							
							if($analogPort!='')
							{						
								$analogPort1 = explode("@",$analogPort);
								for($k=0;$k<count($analogPort1)-1;$k++)
								{						
									$d1 = explode("=",$analogPort1[$k]);
									$analogLabel .= $d1[0]."#";
									$d2 = substr($d1[1],0,strpos($d1[1],"("));
									$d3 = str_replace(")","",substr($d1[1],(strpos($d1[1],"(")+1))); 							
									$d3 = explode("#",$d3);
									for($l = 0; $l < count($d3)-1; $l++)
									{
										$d4 = explode("_",$d3[$l]);
										$fuel_params[$d4[0]] = $d4[1];
									}
									$analogValue = getClosestAnalogValue($fuel_params,splitParameter($d2,$data3[7]))."#".splitParameter($d2,$data3[7]);
									$fuel_params = "";
								}		
							}
							echo $analogLabel.'" analogValue = "'.$analogValue."<br>";	*/
							$xml .='<locations latitude="'.$pos1.'" longitude="'.$pos2. '" speed="'.$mph. '" direction="'.$direction.'" altitute="'.$altitute.'" curTime = "'.$curTime.'" distance="'.floor($totalDistance).'" gpsTime="'.date("h:i:s A",strtotime($data3[9])).'" geodate="'.$geodate.'" deviceIMEI="'.$deviceIMEI. '" deviceName="'.$devName.'" sessionID="'.$sessionID.'"  extraInfo="'.$extraInfo.'"  route="'.$rtName.'"  deviceModel="'.str_replace("&","",$devModel).'" Parameter="'.$data3[7].'" icon = "'.$devIcon.'" />';
							
							//$tmpDist = $travlDist;
							$ct++;
						}
					}
					
				}
			}
			array_push($timeArr,$geoTime);
		
		if($j1>0)				
		{
			$dif = count($data1)-$j1;
			if($dif > 6)
				$j1 = $j1+1;
			else
				$j1 = $j1+1;
		}
		else
		{
			$j1 = $j1+1;
		}
		//$j1 = $j1+1;
		
		}// while
		//echo $totalDistance." ".count($ptArr);
		$xml .= '<OtherData totPt="'.$totPath.'" geoData="" geoPointName="" totalDist="'.$travlDist.'" inPort = "'.$inPort.'" outPort = "'.$outPort.'" analogPort = "'.$analogPort.'"  />';	
		$xml .= '</gps>';	
		header('Content-Type: text/xml');	
		echo $xml;
	}
	else
	{
		$z = GetDays($_GET[sDate],$_GET[eDate]);
		//print_r($z);
		$cont = 1;
		$ptArr = array();
		$timeArr= array();
		$totalDistance=0;
		$xml = '<gps>';
		//echo count($z);
		for($y=0; $y<count($z); $y++)
		{ 
			if($y == 0) 
			{
				$strtTime = $_GET[sTime];
				$endTime = 1439;
			}
			elseif($y == count($z)-1) 
			{
				$strtTime = 0;
				$endTime = $_GET[eTime];
			}
			elseif($y < count($z)-1) 
			{
				$strtTime = 0;
				$endTime = 1439;
			}
			//echo $z[$y]."<br><br>";
			$gpsdata = gpspathFun($_SESSION[clientID],$z[$y],$_GET["deviceIMEI"],$strtTime,$endTime);	
			/*$km = explode("#",liveKmsAllPerDay($gpsdata));
			$totKm += $km[0];
			$totPath .= $km[1];*/
			
			$data1=explode("#",$gpsdata);
			
			$ct = 0;
			$j1=0;
			while($j1<count($data1))		
			{
				$data2=explode("@",$data1[$j1]);
				if(count($data2)>1)
				{
				$data3=explode(",",$data2[1]);
				$geoTime = date("h:i:s A",strtotime($data3[9]));
				$geodate = $data3[8]." ".$geoTime;
				//echo "<br>";				
				$curTime = explode(":",$data3[9]);
				$curTime = (($curTime[0] * 60) + $curTime[1]);
				
				//echo $geoTime." ".$geodate." ".$strtTime.">=".$curTime."<=".$endTime."<br>";
					if($curTime >= $strtTime && $curTime<=$endTime)
					{
					//echo $geoTime." ".$geodate." ".$strtTime." ".$curTime."<=".$endTime."<br>";
						$pos1=calLat($data3[2]);
						$pos2=calLong($data3[1]);
						if($pos1>0 && $pos2>0)
						{
							if(!in_array($geodate,$timeArr))
							{
								$tmpLatLng = $pos1.",".$pos2;
								if(!in_array($tmpLatLng,$ptArr))
								{	
									$ptArr[] = $tmpLatLng;
									//echo $geodate."<br>";
									if($ct==0)
									{
										
										$pits1 =  $pos1;
										$pits2 =  $pos2;
									}
									else
									{
										$pits3 =  $pos1;
										$pits4 =  $pos2;
										$dist = getDistance($pits1, $pits2, $pits3, $pits4);
										$totalDistance += $dist;
										$pits1 =  $pits3;
										$pits2 =  $pits4;
									}
									
									$mph = round($data3[3]);
									$direction = $data3[4];
									$altitute = $data3[5];
									$deviceIMEI = $data3[0];
									$sessionID = $_GET["sessionID"] ;
									$accuracy = $data3[6];
									$extraInfo = $data3[6];
									$travlDist = round($totalDistance);
									
									$xml .='<locations latitude="'.$pos1.'" longitude="'.$pos2. '" speed="'.$mph. '" direction="'.$direction.'" altitute="'.$altitute.'" curTime = "'.$curTime.'" distance="'.floor($totalDistance).'" gpsTime="'.date("h:i:s A",strtotime($data3[9])).'" geodate="'.$geodate.'" deviceIMEI="'.$deviceIMEI. '" deviceName="'.$devName.'" sessionID="'.$sessionID.'"  extraInfo="'.$extraInfo.'"  route="'.$rtName.'"  deviceModel="'.str_replace("&","",$devModel).'" Parameter="'.$data3[7].'" icon = "'.$devIcon.'" />';
									
								}
							}
						}
						$ct++;
					}
				}
			array_push($timeArr,$geodate);
			
			if($j1>3)				
			{
				$dif = count($data1)-$j1;
				if($dif > 6)
					$j1 = $j1+1;
				else
					$j1 = $j1+1;
			}
			else
			{
				$j1 = $j1+1;
			}
			//$j1 = $j1+12;
			}// while
			
			
		}
		$xml .= '<OtherData totPt="'.$totPath.'" geoData="" geoPointName="" totalDist="'.$travlDist.'" inPort = "'.$inPort.'" outPort = "'.$outPort.'" analogPort = "'.$analogPort.'"  />';	
		$xml .= '</gps>';			
		header('Content-Type: text/xml');	
		echo $xml;
	}


}
else
{
	gpspathFunAll($_SESSION[clientID],$_GET["date_offline"],$_GET["deviceIMEI"]);	
}

function gpspathFunAll($clientId,$date_offline,$deviceIMEI)
{
	$date_offline = $_GET[date_offline];
	$path1=DATAPATH.date('d-m-Y',strtotime($date_offline))."/".$deviceIMEI.".txt"; ;
	
	if(chk_folder($path1))
	{
		$file1 = @fopen($path1, "r");
		if($file1)
		{
			$i=0;
			while(!feof($file1))
			{
			  $data1= fgets($file1);				 
			}
			$data1 = getSortedData($data1);
			
			$km = liveKmsAllPerDay($data1);
			//exit;
			  //$data = $data1;
//			$devName = getDeviceName($_GET["deviceIMEI"]);  
			storeData($data1,$km,$_GET["deviceIMEI"],'','');
			fclose($file1);
		}
		else
		{
			$data=0;
			//storeData($data);
		}
	}
		
}
function getDeviceName($imeiNo)
{
	$db = new Database(DB_SERVER, DB_USER, DB_PASS, DB_DATABASE); 
	$db->connect(); 
	$getDevName = "SELECT * FROM tb_deviceinfo,tb_device_model WHERE id = di_deviceModel AND di_imeiId =".$imeiNo." AND di_status = 1";
	
	
	$resultRow = $db->query($getDevName);
	$deviceRecord = $db->fetch_array($resultRow);
	if($deviceRecord[di_deviceName])
		return $deviceRecord[di_deviceName]."^".ucfirst($deviceRecord[tdm_aliasName])."^".$deviceRecord[di_inputPort]."^".$deviceRecord[di_outputPort]."^".$deviceRecord[di_analogPort]."^".$deviceRecord[di_mapIcon];
	else
		return $deviceRecord[di_deviceId]."^".ucfirst($deviceRecord[tdm_aliasName])."^".$deviceRecord[di_inputPort]."^".$deviceRecord[di_outputPort]."^".$deviceRecord[di_analogPort]."^".$deviceRecord[di_mapIcon];
}


function storeData($data,$km,$devImei,$sTime2,$eTime2)
{
	//echo $data;
	$data1=explode("#",$data);
	$latnlong_values="";
	$devData = explode("^",getDeviceName($devImei));  
	$devName = $devData[0];
	$devModel = $devData[1];
	$fuelPort = $devData[2];
	$otherPort = $devData[3];
	//$xml = '<gps geoData="'.$latnlong_values.'" geoPointName="'.$point_names.'">';
	$km = explode("#",$km);
	$totKm += $km[0];
	$totPath .= $km[1];
	//print_r($km);
	//exit;

	$xml = '<gps>';
	$timeArr= array();
	$totalDistance=0;

	$ct = 0;
	$j1=0;
	while($j1<count($data1))
	{
		$data2=explode("@",$data1[$j1]);
		if(count($data2)>1)
		{
			$data3=explode(",",$data2[1]);
			//print_r($data3);
			$date = ($data3[8]/1000);
			$date = $data3[8]." ".$data3[9];
			$geodate = $data3[8]." ".$data3[9];
			$geoTime = $data3[9];

			$pos1=calLat($data3[2]);
			$pos2=calLong($data3[1]);
			if($pos1>0 && $pos2>0)
			{
				
				if(!in_array($geoTime,$timeArr))
				{
					if($j1==0)
					{
						$pits1 =  $pos1;
						$pits2 =  $pos2;
					}
					else
					{
						$pits3 =  $pos1;
						$pits4 =  $pos2;
						
						$dist = getDistance($pits1, $pits2, $pits3, $pits4);
						$totalDistance += $dist;
						$pits1 =  $pits3;
						$pits2 =  $pits4;
					}
					$mph = $data3[3];
					$direction = $data3[4];
					$altitute = $data3[5];
					$deviceIMEI = $data3[0];
					$sessionID = $_GET["sessionID"] ;
					$accuracy = $data3[6];
					$extraInfo = $data3[6];
		
		$xml .='<locations latitude="'.$pos1.'" longitude="'.$pos2. '" speed="'.$mph. '" direction="'.$direction.'" altitute="'.$altitute.'" curTime = "'.$curTime.'" distance="'.round($totalDistance).'" gpsTime="'.date("h:i A",strtotime($geoTime)).'" geodate="'.$geodate.'" deviceIMEI="'.$deviceIMEI. '" deviceName="'.$devName.
	  '" sessionID="'.$sessionID.'"  extraInfo="'.$extraInfo.'"  route="'.$rtName.'" deviceModel="'.str_replace("&","",$devModel).'" other="'.$data3[7].'"  fuelPort="'.$fuelPort.'" otherPort="'.$otherPort.'"/>';						
					}

			}
		}
	array_push($timeArr,$geoTime);
	if($j1>0)				// && $j1 < count($data1))
	{
		$dif = count($data1)-$j1;
		if($dif > 10)
			$j1 = $j1+10;
		else
			$j1 = $j1+1;
		//echo $dif." ".$j1." ".count($data1)."<br>";
	}
	else
	{
		$j1 = $j1+1;
		//echo $dif." ".$j1." ".count($data1)."<br>";
	}
	
	}
	$xml .= '<OtherData totPt="'.$totPath.'" geoData="" geoPointName="" totalDist="'.$totKm.'" />';	
	$xml .= '</gps>';
	
	header('Content-Type: text/xml');
	echo $xml;
}

?>
